package dao.impl;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;



import pojo.FoodType;
import dao.FoodTypeDao;
import util.DBUtil;
import util.DBUtil.ConnType;


public class FoodTypeDaoImpl implements FoodTypeDao{

	@Override
	public List<FoodType> findAllFoodTypes() {
		List<FoodType> list = new ArrayList<FoodType>();
		Connection con = null;
  		PreparedStatement pst = null;
  		ResultSet rs = null;
  
  		String sql = "select * from Food_type";
  		FoodType t = null;
  
  		try {
  			con = DBUtil.createConnection(ConnType.JNDI_ORACLE);
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			
			while(rs.next()){
				t = new FoodType(rs.getString("id"), rs.getString("type"));
				list.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
			return list;
		} 
	}

	@Override
	public int addFoodType(FoodType ft) {
		Connection con = null;
  		PreparedStatement pst = null;
   
  
  		String sql = "insert into food_type(id, type) values(?,?)";
  	 
        int m = 0;
  		try {
  			con = DBUtil.createConnection(ConnType.JNDI_ORACLE);
			pst = con.prepareStatement(sql);
			pst.setString(1, ft.getId());
			pst.setString(2, ft.getName());
			m = pst.executeUpdate();

		} catch (Exception e) {
	 
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
			return 0;
		} 
  		
	}

}
